//
//  LDCountDownButton.m
//  zhilian
//
//  Created by 李丹 on 2019/7/5.
//  Copyright © 2019 LZT. All rights reserved.
//
#import "LDCountDownButton.h"
@interface LDCountDownButton () {
    UIFont *_font;
}
@property (nonatomic, strong)NSTimer *timer;
@property (nonatomic, strong)NSDate *startDate;;

@end
@implementation LDCountDownButton
@dynamic font;

- (void)dealloc {
    [self stopTimer];
}
+(instancetype)createButtonTimeSecond:(NSInteger)second {
    LDCountDownButton *btn = [LDCountDownButton buttonWithType:UIButtonTypeCustom];
    btn.totalSecond = second;
    btn.defautString = @"发送验证码";
    [btn setTitle:btn.defautString forState:0];
    return btn;
}
- (instancetype)init {
    if (self  = [super init]) {
        self.totalSecond = CodeTimeCount;
        [self setTitle:self.defautString forState:0];
//        self.titleLabel.adjustsFontSizeToFitWidth = YES;

    }
    return self;
}

- (UIFont *)font {
    return _font;
}

- (void)setFont:(UIFont *)font {
    _font = font;
    self.titleLabel.font = font;
}

- (NSMutableAttributedString *)attString:(NSNumber *)num{
    if (!self.lastString) {
         self.lastString = @"后可重新获取";
    }
    NSString *allString = [NSString stringWithFormat:@"已发送(%@s)",num];
//    NSString *allString = [NSString stringWithFormat:@"%@s%@",num,self.lastString];
    NSMutableAttributedString * attstring = [[NSMutableAttributedString alloc] initWithString:allString];
//    NSRange range   = [allString rangeOfString:[NSString stringWithFormat:@"%@s",num]];
//    NSRange range1  = [allString rangeOfString:self.lastString];
//    UIFont * ff     = self.font?self.font:LDAutoFont(13);
    [attstring addAttribute:NSFontAttributeName value:TGAutoFont(12) range:NSMakeRange(0, allString.length)];
    [attstring addAttribute:NSForegroundColorAttributeName value:UIColor.fontColorGray2 range:NSMakeRange(0, allString.length)];
//    UIColor *color = self.lastStringTextColor?self.lastStringTextColor:TitleBlackColor;
//    [attstring addAttribute:NSForegroundColorAttributeName value:RedColor range:range];
//    [attstring addAttribute:NSForegroundColorAttributeName value:color range:range1];
    return attstring;
}

- (void)setCodeNormalText {
    NSMutableAttributedString * attstring = [[NSMutableAttributedString alloc] initWithString:@"发送验证码"];
    NSRange strRange = {0,[attstring.string length]};
    [attstring addAttribute:NSForegroundColorAttributeName value:DeepGrayColor range:strRange];
    [attstring addAttribute:NSFontAttributeName value:self.font?self.font:TGAutoFont(13) range:strRange];
    //可以重发
    [self setAttributedTitle:attstring forState:UIControlStateNormal];
}

- (void)setAnewCodeNormalText {
    NSMutableAttributedString * attstring = [[NSMutableAttributedString alloc] initWithString:@"重新发送"];
    NSRange strRange = {0,[attstring.string length]};
    [attstring addAttribute:NSForegroundColorAttributeName value:DeepGrayColor range:strRange];
    [attstring addAttribute:NSFontAttributeName value:self.font?self.font:TGAutoFont(13) range:strRange];
    //可以重发
    [self setAttributedTitle:attstring forState:UIControlStateNormal];
}
///开始倒计时
- (void)startCountDown
{
    [self starTimer];
}
///停止倒计时
- (void)stopCountDown {
    [self stopTimer];
    [self setTitle:self.defautString forState:UIControlStateNormal];
    self.userInteractionEnabled = YES;
}
#pragma mark 定时器开关
- (void)starTimer
{
    [self stopTimer];
    self.startDate = [NSDate date];
    [self timerFired:nil];
    self.userInteractionEnabled = NO;
    _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:[YYWeakProxy proxyWithTarget:self] selector:@selector(timerFired:) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop]addTimer:_timer forMode:NSRunLoopCommonModes];
}
- (void)stopTimer
{
    [_timer invalidate];
    _timer = nil;
}
- (void)checkTimer {
    NSInteger cha = [self now:[NSDate date] before:self.startDate] + 1;
    if (cha < self.totalSecond){
        [self timerFired:nil];
        [self starTimer];
    }
}
- (void)setType:(LDCountType)type{
    _type = type;
    [self setCodeNormalText];
}
#pragma mark 触发事件
//定时器
- (void)timerFired:(NSTimer *)timer
{
    
    NSInteger cha = [self now:[NSDate date] before:self.startDate];
    if (cha < self.totalSecond)
    {
        
        if (self.type == LDCountTypeAttribute) {
            [self setAttributedTitle:[self attString:@(self.totalSecond - cha)] forState:UIControlStateNormal];
        }else {
            NSString * str = [NSString stringWithFormat:@"%@%@%@",R(self.preString),@(self.totalSecond - cha),R(self.lastString)];
            //限制重发
            [self setTitle:str forState:UIControlStateNormal];
        }
     
        self.userInteractionEnabled = NO;
    }
    else
    {
        if (self.type == LDCountTypeAttribute) {
            [self setAnewCodeNormalText];
        }else {
            [self setTitle:self.defautString forState:UIControlStateNormal];

        }
        //可以重发
        self.userInteractionEnabled = YES;
        [self stopTimer];
    }
}

- (NSInteger)now:(NSDate *)now before:(NSDate *)before
{
    NSTimeInterval late = [before timeIntervalSince1970] * 1;
    NSTimeInterval nowInt = [now timeIntervalSince1970] * 1;
    NSTimeInterval cha = nowInt - late;
    return fabs(cha);
}
@end
